<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Repositories\RenewRepository;
use App\Repositories\OrderRepository;
use App\Repositories\DepartmentRepository;
use App\Repositories\UserRepository;
use App\Services\PHPTree;
use App\Http\Requests\RenewRequest;
use App\Repositories\ContractRepository;


class RenewController extends AdminController
{

    public function __construct(
        RenewRepository $renew, 
        OrderRepository $order, 
        DepartmentRepository $department,
        UserRepository $user,
        ContractRepository $contract
    )
    {
        $this->renew = $renew;
        $this->order = $order;
        $this->department = $department;
        $this->user = $user;
        $this->contract = $contract;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $where = collect($request->except('page'));

        $data = $this->renew->paginateList($where);

        return view('admin.renew.index', compact('where', 'renews', 'data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create(Request $request)
    {
        $orderId = $request->input('order_id');

        $order = $this->order->find($orderId);

        // 销售选择
        $tree = new PHPTree($this->department->all()->toArray());
        $departmentsTree = $tree->render();
        $salers = $this->user->getSalersByDepartment();

        return view('admin.renew.add', compact('order', 'departmentsTree', 'salers'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(RenewRequest $request)
    {
        $order = $this->order->find($request->input('order_id'));

        if (!$order) {
            return back()->with('error', '您要续费的订单不存在');
        }

        // 检查合同完整性
        $request->input('contract_no') && ($this->contract->checkFull($request->input('contract_no'), $order->cid));

        $result = $this->renew->create($request->all());

        if ($result) {

            // 更新订单的到期日期
            $order = $result->order;
            $order->expired_at = $order->actualed_at->addYears($order->year + $order->renews->sum('year'))->endOfDay();
            $order->save();

            return redirect($request->input('referer'))->with('success', '订单续费成功');
        }

        return back()->with('error', '订单续费失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $renew = $this->renew->find($id);
        return view('admin.renew.edit', compact('renew', 'departmentsTree', 'salers'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        $result = $this->renew->updateFind($request->all(), $id);

        if ($result) {
            return redirect()->route('admin.renew.index')->with('success', '续费信息更新成功'); 
        }

        return back()->with('error', '续费信息更新失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Request $request, $id)
    {
        $renew = $this->renew->find($id);

        if (!$renew) {
            flash('error', '您要删除的续费不存在。');
            return response()->json(['code' => 500]);
        }

        $result = $renew->delete();

        if ($result) {
            // 更新订单的到期日期
            $order = $renew->order;
            $order->expired_at = $order->actualed_at->addYears($order->year + $order->renews->sum('year'))->endOfDay();
            $order->save();

            flash('success', '删除的续费成功');
            return response()->json(['code' => 200]);
        }

        flash('error', '删除续费失败。');
        return response()->json(['code' => 500]);
    }
}